<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Date" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@taglib uri="/roomUtil"  prefix="roomUtil"%>
<%@taglib uri="/adminUtil"  prefix="adminUtil"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<c:if test="${empty requestScope.checkIns }">
			<h1>没有类型信息</h1>
	</c:if>
	<c:if test="${!empty requestScope.checkIns }">
		<table>
			<tr>
				<th>房间名称</th>
				<th>处理人</th>
				<th>入住人联系方式</th>
				<th>入住时间</th>
				<th>退房时间</th>
				<th>收费</th>
				<th>备注</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${requestScope.checkIns }" var="checkIn">
				<tr>
					<td>${roomUtil:getRoomName(checkIn.roomId)}</td>
					<td>${adminUtil:getAdminName(checkIn.adminId)}</td>
					<td>${checkIn.phone }</td>
					<td>${checkIn.inTime }</td>
					<td>${checkIn.outTime }</td>
					<td>${checkIn.cost }</td>
					<td>${checkIn.zcomment }</td>
					<td>
						<button  onclick="leaveRoom(${checkIn.id })">退房</button>
						<button  onclick="location.href='/hotel/changeRoom/changePage?id=${checkIn.id}'">换房</button>
						<button  onclick="location.href='/hotel/continueBook/continuePage?id=${checkIn.id}'">续住</button>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>

<button id="offBut">未预定入住</button><br>
<button id="onBut">已预订入住</button><br>
<div id="offDiv">
	房间类型:<select id="typeId"></select> <br>
	房间名称:<select id="room"></select>	<br>
	价格:<label id='price'></label><br>
	<input type="hidden" id="calcPrice">
	图片:<img id='pic'><br>
	添加时间:<label id='addDate'></label><br>
	备注	
	<div id='zcomment'></div>
	<br>
	<input type="hidden" id="roomId">
	<button id="lock">锁定该房间</button>
	<br>
	<%
		Date now=new Date();
		Date nextDay=new Date(now.getTime()+3600*24*1000);//一天后
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
		String nowStr=format.format(now);
		String nextStr=format.format(nextDay);
	%>
	电话号码:<input type="text" id="phone" value='18340011234'><br>
	身份证号:<input type="text" id="idCardNo" value='622425199711181234'><br>
	入住时间:<input type="text" id="inTime" value='<%=nowStr %>' > <br>
	退房时间:<input type="text" id="outTime" value='<%=nextStr %>'><br>
	收费:<label id='cost'></label><br>
	备注:
	<textarea cols="20" rows="20" id="checkInComment"></textarea>
	<button id='sub'>入住办理</button>
	<button id='calc'>计算</button>
</div>
<div id="onDiv">
	<div id="onInputDiv">
		订单id:<input type="text" id="reserveId"><br>
		订单密码：<input type="text" id="reservePass"><br>
		<button id="onResBut">提交</button>
	</div>
	<div id="reserveInforDiv">
		订单编号:<label id="onOrderId"></label><br>
		总金额:<label id="onCost"></label><br>
		房间名称:<label id="onRoomName"></label><br>
		预定入住日期:<label id="onOriInDate"></label><br>
		预定离店日期:<label id="onOutDate"></label><br>
		联系电话:<input type="text" id="resPhone"><br>
		身份证号:<input type="text" id="resIdCardNo"><br>
		备注:<textarea cols='20' rows='20' id='resComment'></textarea><br>
		<button id='onCheckBut'>入住</button>&nbsp;&nbsp;<button id='onCancelBut'>取消</button>
	</div>
</div>
<br>
<style>
	#offDiv{
		display:none;
	}
	#onDiv{
		display:none;
	}
	#reserveInforDiv{
		display:none;
	}
</style>
<script type="text/javascript" src="/hotel/js/jquery.min.js"></script>
<script type="text/javascript" src="/hotel/js/timeUtil.js"></script>
<script type="text/javascript">
	function clearRoomInfor(){
		$('#roomId').val('')
		$('#price').text('')
		$('#pic').attr('src','')
		$('#addDate').text('')
		$('#zcomment').text('')
	}
	//更新房间信息
	function refreshRoomInfor(id){
		if(id!=null){
			$.get("/hotel/checkIn/roomInfor",{'id':id},function(str){
				//alert(str)
				var res=eval('(' +str + ')')
				if(res.code!=0){
					alert(res.message)
					return;
				}
				room=res.object
				$('#roomId').val(room.id)
				$('#price').text(room.price+'元/日(房型折扣:'+room.typeDiscount+'%,房间折扣:'+room.discount+'%)')
				$('#calcPrice').val(room.price)
				$('#pic').attr('src','/hotel/upload/'+room.pic)
				$('#addDate').text(room.addDate)
				$('#zcomment').text(room.zcomment)
			})
		}
	}
	//更新类型选择列表
	function refreshTypeList(){
		$('#typeId').empty()
		$('#room').empty()
		$.get("/hotel/checkIn/typeList",{},function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				return;
			}
			types=res.object
			for(var i=0;i<types.length;i++){
				//alert(types[i].id+' '+types[i].name)
				$('#typeId').append('<option value='+types[i].id+'>'+types[i].name+'</option>')
			}
		})
	}
	
	//更新房间选择列表
	function refreshRoomList(id){
		$('#room').empty()
		$.get("/hotel/checkIn/freeRoomList",{'id':id},function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				return;
			}
			rooms=res.object
			for(var i=0;i<rooms.length;i++){
				//alert(types[i].id+' '+types[i].name)
				$('#room').append('<option value='+rooms[i].id+'>'+rooms[i].floor+'F-'+rooms[i].name+'</option>')
			}
			//$('#room').val(rooms[0].id)
			if(rooms.length>0){//如果有符合条件的房间
				refreshRoomInfor(rooms[0].id)	//更新房间信息
			}
		})
	}
	//锁定某房间
	function lockRoom(id,code){
		$.post("/hotel/checkIn/lockRoom",{'id':id,'code':code},function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				refreshTypeList()
				return;
			}
		})
	}
	//计算并展示价格
	function showPrice(){
		str1=$('#inTime').val();
		str2=$('#outTime').val();
		//算法有问题
		price=getDurDays(str1,str2)*$('#calcPrice').val()	//普通房价格计算法
		//price=getDurSeconds(str1,str2)/(24.0 *3600)*$('#calcPrice').val()		 //秒数/24.0/3600*price
		//price=Math.round(price)
		$('#cost').text(price+'元')
	}
	function submitInfor(){
		
	}
	$('#typeId').change(function(){
		clearRoomInfor()
		refreshRoomList($('#typeId').val())
	})
	$('#room').change(function(){
		refreshRoomInfor($('#room').val())
	})
	$('#lock').click(function(){
		if($('#lock').text()=='锁定该房间'){
			$('#lock').text('解锁该房间')
			lockRoom($('#roomId').val(),1)
		}else{
			$('#lock').text('锁定该房间')
			lockRoom($('#roomId').val(),0)
		}
	})
	$('#calc').click(function(){
		showPrice()
	})
	$('#sub').click(function(){
		data={'phone':$('#phone').val(),'idCardNo':$('#idCardNo').val(),'dateStr1':$('#inTime').val(),'dateStr2':$('#outTime').val(),
				'zcomment':$('#checkInComment').text(),'roomId':$('#roomId').val()}
		//alert(JSON.stringify(data))
		$.post("/hotel/checkIn/offlineCheck",data,function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				//refreshTypeList()
				return;
			}else{
				window.location.href="/hotel/system/mainPage"
			}
		})
	})
	$("#offBut").click(function(){
		$('#onDiv').css('display','none')
		$('#offDiv').css('display','block')
	})
	$("#onBut").click(function(){
		$('#onDiv').css('display','block')
		$('#offDiv').css('display','none')
	})
	$("#onResBut").click(function(){
		$.post("/hotel/checkIn/getReserve",{'id':$("#reserveId").val(),'password':$("#reservePass").val()},function(str){
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				return;
			}else{
				res=res.object
				$('#onInputDiv').css('display','none')
				$('#reserveInforDiv').css('display','block')
				$('#onOrderId').text(res.orderId)
				$('#onCost').text(res.price)
				$('#onOriInDate').text(res.inDate)
				$('#onOutDate').text(res.outDate)
				$('#resPhone').val(res.phone)
				$('#resComment').val(res.zcomment)
			}
		})
	})
	$('#onCancelBut').click(function(){
		$('#onInputDiv').css('display','block')
		$('#reserveInforDiv').css('display','none')
	})
	$('#onCheckBut').click(function(){
		data={'orderId':$('#onOrderId').text(),'phone':$('#resPhone').val(),'idCardNo':$('#resIdCardNo').val(),
				'zcomment':$('#resComment').val()
		}
		$.post("/hotel/checkIn/onlineCheck",data,function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				//refreshTypeList()
				return;
			}else{
				window.location.href="/hotel/system/mainPage"
			}
		})
	})
	function leaveRoom(id){
		$.post("/hotel/checkIn/leaveRoomConfirm",{id:id},function(str){
			//alert(str)
			var res=eval('(' +str + ')')
			if(res.code!=0){
				alert(res.message)
				//refreshTypeList()
				return;
			}else{
				res=res.object
				cho=confirm('当前时间为'+res.curOutTime+'\n预计退房时间为'+res.oriOutTime+'\n要补收'+res.price+'元\n是否退房？');
				if(cho){	//如果确定要退房
					$.post("/hotel/checkIn/leaveRoom",{id:res.id},function(str){
						//alert(str)
						var res=eval('(' +str + ')')
						if(res.code!=0){
							alert(res.message)
							return;
						}else{
							window.location.href="/hotel/system/mainPage"
						}
					})
				}
			}
		})
	}
	
	refreshTypeList()
	
	
</script>
